// shop/cart/cart.js
Page({
  data: {
    id: wx.getStorageSync('userid'),
    list:[],
    dingdan:'',
    allprice:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id:wx.getStorageSync('userid')
    })
    wx.request({
      url: `https://www.prebeauty.work:3001/api/cart/${this.data.id}`,
      header:{
        'content-type':'application/json'
      },
      success : res => {
        console.log(res.data);
        if(res.data.length == 0){
          wx.showModal({
            title: '礼盒是空的！！',
            content: '去挑选礼物吧',
            success(res){
              if(res.confirm){
                console.log('确定')
                wx.switchTab({
                  url: '/pages/shop/shop',
                })
              }else if(res.cancel){
                console.log('取消')
                wx.switchTab({
                  url: '/pages/shop/shop',
                })
              }
            }
          })
        }
        this.setData({
          list:res.data
        })
        var i=0;
        for(i in res.data){
          this.setData({
            allprice:Number(this.data.allprice)+Number(res.data[i].goodsprice)
          })
        }
        console.log(this.data.allprice)
      }
    })
    console.log(this.data)
  },

  onShow: function () {
    if(!wx.getStorageSync('token')){
      wx.showModal({
        title: '用户未登录',
        content: '请先进行授权登录',
        success(res) {
          if (res.confirm) {
            console.log('确定')
            wx.switchTab({
              url: '/pages/home/home'
           })
          } else if (res.cancel) {
            console.log('取消')
            wx.switchTab({
              url: '/pages/shop/shop'
           })
          }
        }
      })
    }
  },

  delgift:function(e){
    console.log(e.currentTarget.dataset.id)
    wx.request({
      url: `https://www.prebeauty.work:3001/api/cart/${e.currentTarget.dataset.id}`,
      header: {
        'content-type': 'application/json'
      },
      method: 'DELETE',
      success:(res)=>{
        wx.request({
          url: `https://www.prebeauty.work:3001/api/cart/${wx.getStorageSync('userid')}`,
          header:{
            'content-type':'application/json'
          },
          success : res => {
            console.log(res.data);
            this.setData({
              list:res.data
            })
          }
        })
        console.log(this.data)
      }
    })
  },

  tobuy:function(e){
    console.log(e.currentTarget.dataset)
    wx.request({
      url: 'https://www.prebeauty.work:3001/api/dingdan',
      data: {
        goodsid:e.currentTarget.dataset.goodsid,
        goodsname:e.currentTarget.dataset.goodsname,
        price:e.currentTarget.dataset.price,
        pay:'未支付',
        goodsimg:e.currentTarget.dataset.goodsimg,
        goodsnum:1,
        userid:this.data.id,
        type:1
      },
      header: {
        'content-type': 'application/json'
      },
      method: 'POST',
      success: (res)=>{
        console.log(res.data)
        console.log('create dingdan')
        this.setData({
          dingdan: res.data
        })
        console.log(this.data)
        wx.navigateTo({
          url: `/shop/buy/buy?id=${this.data.dingdan}`,
        })
        wx.request({
          url: `https://www.prebeauty.work:3001/api/cart/${e.currentTarget.dataset.cartid}`,
          header: {
            'content-type': 'application/json'
          },
          method: 'DELETE',
          success:(res)=>{
            wx.request({
              url: `https://www.prebeauty.work:3001/api/cart/${wx.getStorageSync('userid')}`,
              header:{
                'content-type':'application/json'
              },
              success : res => {
                console.log(res.data);
                this.setData({
                  list:res.data
                })
              }
            })
            console.log(this.data)
          }
        })
      }
    })
  },

  tosong:function(e){
    console.log(e.currentTarget.dataset)
    wx.request({
      url: 'https://www.prebeauty.work:3001/api/dingdan',
      data: {
        goodsid:e.currentTarget.dataset.goodsid,
        goodsname:e.currentTarget.dataset.goodsname,
        price:e.currentTarget.dataset.price,
        pay:'未支付',
        goodsimg:e.currentTarget.dataset.goodsimg,
        goodsnum:1,
        userid:this.data.id,
        type:2
      },
      header: {
        'content-type': 'application/json'
      },
      method: 'POST',
      success: (res)=>{
        console.log(res.data)
        console.log('create dingdan')
        this.setData({
          dingdan: res.data
        })
        console.log(this.data)
        wx.navigateTo({
          url: `/shop/buy/buy?id=${this.data.dingdan}`,
        })
        wx.request({
          url: `https://www.prebeauty.work:3001/api/cart/${e.currentTarget.dataset.cartid}`,
          header: {
            'content-type': 'application/json'
          },
          method: 'DELETE',
          success:(res)=>{
            wx.request({
              url: `https://www.prebeauty.work:3001/api/cart/${wx.getStorageSync('userid')}`,
              header:{
                'content-type':'application/json'
              },
              success : res => {
                console.log(res.data);
                this.setData({
                  list:res.data
                })
              }
            })
            console.log(this.data)
          }
        })
      }
    })
  }
})